package com.dcivision.mail.core;


import java.io.File;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.Utility;

//Referenced classes of package com.dci.dms.util.client:
//PropertyGetter, HttpSender, HtmlFormText, HtmlFormFile

public class EmailArchiveClearCacheFile {
  
  public static final String REVISION = "$Revision: 1.5 $";
  
  protected Log log = LogFactory.getLog(this.getClass().getName());  
  
  protected static final int LIFECYCLE_DAYS = 7; 
  
  public EmailArchiveClearCacheFile(){
    
  }
  
  /**
   * Delete file when the file is out off lifecycle.
   */
  public void clearCacheFile() {
    String folderName = SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_EMAILARCHIVE_CACHEFOLDER_PATH);
    long expiredDateTime = new Date().getTime() - (LIFECYCLE_DAYS * 86400000);
    
    if (!Utility.isEmpty(folderName)) {
      File catcheFolder = new File(folderName);
      File[] listFile = null;
      if (catcheFolder.exists()) {
        listFile = catcheFolder.listFiles();
        if (!Utility.isEmpty(listFile)) {
          for(int i = 0 ; i< listFile.length ;i++) {
            File file  = listFile[i];
            // in cache folder, create day equals modify day, use modify day as create day.
            if (file.lastModified()<expiredDateTime) {
              file.delete();
            }
            file = null;
          }   		  
        }   	  
      }  
    }
  }
  
  /*
  public void clearCacheFile(){
    // delete file when the file is old than current time 7 days
    long currentDateTime = new Date().getTime();        
    String folderName = SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_EMAILARCHIVE_CACHEFOLDER_PATH);
//  String folderName = "D:\\ParaDM\\file\\ParaDOC\\cache";
    File catcheFolder = new File(folderName);
    File[] listFile;
    if (catcheFolder.exists()) {
      listFile = catcheFolder.listFiles();
      if(!Utility.isEmpty(listFile)){
        for(int i = 0 ; i< listFile.length ;i++){
          File file  = listFile[i];
          String fileName = file.getName();
          String[] fileNameArr = TextUtility.splitString(fileName,"_");
          String fileNameTimeStr = fileNameArr[1];
          long fileNameTime = TextUtility.parseLong(fileNameTimeStr);
          Timestamp tdate = new Timestamp(fileNameTime);
          tdate = Utility.addDay(tdate,7);          
          //delete cache file
          if(currentDateTime > tdate.getTime()){
            file.delete();
          }
          file = null;
        }         
      }       
    }  
  }
  */
  
  public static void main(String args[]) throws Exception  {
    EmailArchiveClearCacheFile cat = new EmailArchiveClearCacheFile();
    cat.clearCacheFile();
  }
  
  
}